import pygame

import os
from pygame.locals import *
from sys import exit
import creditos, instrucoes, tetrix

os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()

screen_width, screen_height = 640, 480

screen = pygame.display.set_mode((screen_width,screen_height), 0, 32)
pygame.display.set_caption("Crazy Boxes")

					# MENU

#Botao iniciar
iniciar_buttons = [pygame.image.load("botoes" + os.sep + "iniciar" + str(i+1) + ".png").convert()\
                for i in range(3)]
iniciar_button = iniciar_buttons[0]
iniciar_size = iniciar_button.get_size()
iniciar_pos = (screen_width/2 - iniciar_size[0]/2, 1.2*screen_height/3 - iniciar_size[1]/2)
#-----------------------------------------------------------------------------------------------------

#Botao instrucao
instruc_buttons = [pygame.image.load("botoes" + os.sep + "instruc" + str(i+1) + ".png").convert()\
                for i in range(3)]
instruc_button = instruc_buttons[0]
instruc_size = instruc_button.get_size()
instruc_pos = (screen_width/2 - instruc_size[0]/2, 1.7*screen_height/3 - instruc_size[1]/2)
#------------------------------------------------------------------------------------------------------

#Botao credito
credito_buttons = [pygame.image.load("botoes" + os.sep + "credito" + str(i+1) + ".png").convert()\
                for i in range(3)]
credito_button = credito_buttons[0]
credito_size = credito_button.get_size()
credito_pos = (screen_width/2 - credito_size[0]/2, 2.2*screen_height/3 - credito_size[1]/2)
#------------------------------------------------------------------------------------------------------
#Botao sair
sair_buttons = [pygame.image.load("botoes" + os.sep + "sair" + str(i+1) + ".png").convert()\
                for i in range(3)]
sair_button = sair_buttons[0]
sair_size = sair_button.get_size()
sair_pos = (screen_width/2 - sair_size[0]/2, 2.7*screen_height/3 - sair_size[1]/2)
#------------------------------------------------------------------------------------------------------

logotipo = pygame.image.load("imagens" + os.sep + "logo1.jpg").convert()

pressed = False

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

    mouse_pos = pygame.mouse.get_pos()
    mouse_press = pygame.mouse.get_pressed()

    # Iniciar
    if iniciar_pos[0] <= mouse_pos[0] <= iniciar_pos[0] + iniciar_size[0]\
       and iniciar_pos[1] <= mouse_pos[1] <= iniciar_pos[1] + iniciar_size[1]:

        iniciar_button = iniciar_buttons[1]

        if mouse_press[0]:
            iniciar_button = iniciar_buttons[2]
            pressed = True

        if pressed and not mouse_press[0]:
            iniciar_button = iniciar_buttons[1]
            tetrix.main()    
           
    else:
        iniciar_button = iniciar_buttons[0]

#------------------------------------------------------------------------------------------------------

    # Instrucao
    if instruc_pos[0] <= mouse_pos[0] <= instruc_pos[0] + instruc_size[0]\
        and instruc_pos[1] <= mouse_pos[1] <= instruc_pos[1] + instruc_size[1]:

        instruc_button = instruc_buttons[1]

        if mouse_press[0]:
            instruc_button = instruc_buttons[2]
            pressed = True

        if pressed and not mouse_press[0]:
            instruc_button = instruc_buttons[1]
            instrucoes.main()
            
    else:
        instruc_button = instruc_buttons[0]
#------------------------------------------------------------------------------------------------------

    # Creditos
    if credito_pos[0] <= mouse_pos[0] <= credito_pos[0] + credito_size[0]\
       and credito_pos[1] <= mouse_pos[1] <= credito_pos[1] + credito_size[1]:

        credito_button = credito_buttons[1]

        if mouse_press[0]:
            credito_button = credito_buttons[2]
            pressed = True

        if pressed and not mouse_press[0]:
            credito_button = credito_buttons[1]
            creditos.main()
            
    else:
        credito_button = credito_buttons[0]
#------------------------------------------------------------------------------------------------------
        
    # Sair
    if sair_pos[0] <= mouse_pos[0] <= sair_pos[0] + sair_size[0]\
       and sair_pos[1] <= mouse_pos[1] <= sair_pos[1] + sair_size[1]:

        sair_button = sair_buttons[1]

        if mouse_press[0]:
            sair_button = sair_buttons[2]
            pressed = True

        if pressed and not mouse_press[0]:
            sair_button = sair_buttons[1]
            exit()
            
    else:
        sair_button = sair_buttons[0]

    if not mouse_press[0]:
        pressed = False

    screen.blit(logotipo, (0,0))
    screen.blit(credito_button, credito_pos)
    screen.blit(instruc_button, instruc_pos)
    screen.blit(iniciar_button, iniciar_pos)
    screen.blit(sair_button, sair_pos)
    
    
    pygame.display.update()
            
